<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-input
        placeholder="请输入内容"
        style="width: 400px"
        v-model="queryInfo.query"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="searchGoods"
        ></el-button>
      </el-input>
      <!-- 添加商品按钮 -->
      <el-button type="primary" class="addBtn" @click="showAddGoods"
        >添加商品</el-button
      >

      <!-- 表格 -->
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="160px">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="160px">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="300px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="460px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="showEditDialog(scope.row)"
              >操作</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="deleteGoods(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改商品对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editFromClosed"
    >
      <!-- 表单 -->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
      >
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="goods_number">
          <el-input v-model="editForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editFormData">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "GoodsList",
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
      goodsList: [],

      // 编辑商品
      editDialogVisible: false,
      editForm: {},
      editFormRules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
      },
      editId: "",
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    //   获取商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) return this.$message.error("获取失败");

      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    // 查找商品
    searchGoods() {
      this.getGoodsList();
    },
    // 展示添加商品的对话框
    showAddGoods() {
      this.$router.push("/add");
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodsList();
    },
    // 开启编辑商品对话框
    showEditDialog(row) {
      this.editDialogVisible = true;
      this.editId = row.goods_id;
      this.editForm = row;
      this.editForm.goods_cat = "1,3,6";
    },
    // 提交修改的数据
    editFormData() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return this.$message.error("请填写完整的表格");

        const { data: res } = await this.$http.put(
          `goods/${this.editId}`,
          this.editForm
        );

        if (res.meta.status !== 200) return this.$message.error("编辑失败");

        this.$message.success("编辑商品成功");
        this.editDialogVisible = false;
        this.getGoodsList();
      });
    },
    // 关闭修改对话框
    editFromClosed() {
      this.$refs.editFormRef.resetFields();
    },
    // 删除商品
    async deleteGoods(row) {
      const confirmStr = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      if (confirmStr !== "confirm") return this.$message.info("取消了删除操作");

      const { data: res } = await this.$http.delete(`goods/${row.goods_id}`);
      if (res.meta.status !== 200) return this.$message.error("删除失败");
      this.$message.success("删除成功");
      this.getGoodsList();
    },
  },
};
</script>

<style scoped>
.addBtn {
  margin-left: 25px;
}
</style>